package com.google.code.p.restrpc.http;

import java.util.Properties;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

//http://hc.apache.org/httpcomponents-client-ga/tutorial/html/
public class HttpClientManager {
    
    public static final String httpclient_timeout = "httpclient.timeout";
    
    public static final String httpclient_so_timeout = "httpclient.so.timeout";
    
    public static final String httpclient_connection_timeout = "httpclient.connection.timeout";
    
    /**
     * @param properties
     * @return
     */
    public static HttpClient newInstance(Properties properties) {
        BasicHttpParams httpParams = new BasicHttpParams();
        
        ConnManagerParams.setTimeout(httpParams, 1000);
        HttpConnectionParams.setSoTimeout(httpParams, 1000);
        HttpConnectionParams.setConnectionTimeout(httpParams, 1000);
        
        SchemeRegistry schReg = new SchemeRegistry();
        schReg.register(new Scheme("http",
                                   80,
                                   PlainSocketFactory.getSocketFactory()));
        schReg.register(new Scheme("https",
                                   443,
                                   SSLSocketFactory.getSocketFactory()));
        ClientConnectionManager conMgr = new ThreadSafeClientConnManager();
        return new DefaultHttpClient(conMgr, httpParams);
    }
}
